module game
{
	/**
	 * 基础泡泡
	 * 所有pop去检查自己下面的格子是否还有pop，没有pop就往下掉
	 */
	export class BasePop extends egret.DisplayObjectContainer
	{
		private _img:eui.Image;
		private _selectImg:eui.Image;
		private _curPt:egret.Point;
		/**目标点 */
		private _tagPt:egret.Point;

		/**用于测试 */
		private _text:eui.Label;

		public data:PopData;

		public constructor(isBlank:boolean = false)
		{
			super();
			this.data = new PopData();
			this.data.isBlank = isBlank;

			this._img = new eui.Image();
			this._img.width = 38;
			this._img.height = 38;
			this.addChild(this._img);
			this._img.x = 1;
			this._img.y = 1;

			this._text = new eui.Label();
			// this.addChild(this._text);
			this._text.size = 20;
			this._text.text = "00";
			this._text.x = 9;
			this._text.y = 10;
		}

		/**
		 * 是否是空白位
		 */
		public get isBlank():boolean
		{
			return this.data.isBlank;
		}

		public set isBlank(value:boolean)
		{
			this.data.isBlank = value;
			if(value)
			{
				//空白为会不显示
				this.visible = false;
				// this.alpha = 0.6
				this.type = -1;
			}
		}

		/**
		 * 是否是已经闲置
		 */
		public get isIdle():boolean
		{
			return this.data.isBlank;
		}

		public set isIdle(value:boolean)
		{
			this.data.isIdle = value;
		}

		/**pop类型 */
		public set type(value:number)
		{
			this.data.type = value;
			if(value >= 0)
			{
				this._img.source = "e" + value + "_png";
			}
		}

		public get type():number
		{
			return this.data.type;
		}

		/**pop的目标点 */
		public set tagPt(value:egret.Point)
		{
			if(value.x==5 && value.y==6)
			{
				console.warn("why~~~~~~~");
			}
			this._tagPt = value;
			this._text.text = value.x + "" + value.y;
			this._text.textColor = 0;
			// this.move(value);
		}

		public get tagPt():egret.Point
		{
			return this._tagPt;
		}

		/**pop的当前点 */
		public set curPt(value:egret.Point)
		{
			this.data.posX = value.x;
			this.data.posY = value.y;
			this._curPt = value;
			this._text.text = value.x + "" + value.y;
			this._text.textColor = 0xffffff;
		}

		public get curPt():egret.Point
		{
			return this._curPt;
		}

		/**
		 * 移动到目标点
		 * 根据距离计算动画时间
		 */
		public move():void
		{
			if(this._tagPt && !this._tagPt.equals(this._curPt))
			{
				let pt = this._tagPt
				let tx = (pt.x - this.curPt.x) * 40 + this.x;
				let ty = (pt.y - this.curPt.y) * 40 + this.y;
				let sp = Math.abs(pt.x - this.curPt.x) + Math.abs(pt.y - this.curPt.y);
				let dis = egret.Point.distance(this._curPt,this._tagPt);
				console.log("move distance:",Math.sqrt(dis).toFixed(1));
				let sdis = +Math.sqrt(dis).toFixed(1);
				// console.warn("tx ty",tx,ty);
				egret.Tween.get(this).to({x:tx,y:ty},sdis*200).call(this.onComplete,this);
				TweenManager.getInstance().start();
			}
		}

		/**
		 * 固定的动画
		 */
		private onComplete():void
		{
			this.curPt = this.tagPt;
			if(this.x/40 != this.curPt.x || this.y/40 != this.curPt.y)
			{
				console.error("坐标不等...",this.x/40,this.y/40,this.curPt.x,this.curPt.y);
			}
			TweenManager.getInstance().complete();
		}

		public clear():void
		{

		}
	}
}